/*
 * $Id: ActRequestListUserDAO.java,v 1.10 2009/09/14 18:54:22 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.database.dao;

import java.util.ArrayList;
import java.util.List;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.ActRequest;
import com.ge.healthcare.autosc.common.database.pojo.ActRequestList;
import com.ge.healthcare.autosc.common.database.pojo.ActRequestListUser;
import com.ge.healthcare.autosc.common.database.pojo.ActTask;
import com.ge.healthcare.autosc.common.database.pojo.CfgCodeDetails;

/**
 * @author 212042764
 * 
 */
public class ActRequestListUserDAO extends BaseDAO implements
		IRequestListUserDAO {
	/**
	 * Eclispe generated
	 */
	private static final long serialVersionUID = 2195881952291866740L;

	public ActRequestListUserDAO() {
		super(ActRequestListUser.TABLE_NAME);
	}

	public ActRequestListUser getByUserID(Long requestListUserId) {
		Object object = findObjectByID(ActRequestListUser.COL_REQUEST_USER_ID,
				requestListUserId);
		if (!ASCUtil.isNullOrEmptyObject(object)) {
			return (ActRequestListUser) object;
		}
		return null;
	}

	public List<ActRequestListUser> getByRequestListID(Long requestListId) {
		List<?> rtvdUsers = findByProperty(
				ActRequestListUser.COL_REQUEST_LIST_ID, requestListId);
		if (ASCUtil.isNullOrEmptyList(rtvdUsers)) {
			return null;
		}

		List<ActRequestListUser> userLst = new ArrayList<ActRequestListUser>();
		for (Object o : rtvdUsers) {
			userLst.add((ActRequestListUser) o);
		}
		return userLst;
	}

	public List<ActRequestListUser> getNotifyRecipients(ActTask task) {

		List<ActRequestListUser> recipients = null;
		if (!ASCUtil.isNullOrEmptyObject(task.getRequestId())) {
			StringBuffer msg = new StringBuffer("Notify Email To RequestId:");
			msg.append(task.getRequestId());

			List<ActRequestListUser> replyTo = new ArrayList<ActRequestListUser>();
			List<ActRequestListUser> sentFrom = new ArrayList<ActRequestListUser>();
			StringBuffer query = new StringBuffer("Select ");
			query.append(" user, code ");
			query.append(" from ");
			query.append(ActRequestListUser.TABLE_NAME).append(" as user , ");
			query.append(ActRequest.TABLE_NAME).append(" as request , ");
			query.append(ActRequestList.TABLE_NAME).append(" as rqList , ");
			query.append(CfgCodeDetails.TABLE_NAME).append(" as code ");
			query.append(" where ");
			query.append(" user.").append(ActRequestListUser.COL_CODE_ID);
			query.append(" = ");
			query.append(" code.").append(CfgCodeDetails.COL_CODE_ID);
			query.append(" and ");
			query.append(" user.").append(
					ActRequestListUser.COL_REQUEST_LIST_ID).append(" = ");
			query.append(" rqList.").append(ActRequestList.COL_REQUEST_ID);
			query.append(" and ");
			query.append(" request.").append(ActRequest.COL_REQUEST_LIST_ID);
			query.append(" = ");
			query.append(" rqList.").append(ActRequestList.COL_REQUEST_ID);
			query.append(" and ");
			query.append(" request.").append(ActRequest.COL_REQUEST_ID);
			query.append(" = ?");
			try {
				List<?> values = (ArrayList<?>) getHibernateTemplate().find(
						query.toString(), new Object[] { task.getRequestId() });
				if (!ASCUtil.isNullOrEmptyList(values)) {
					for (int i = 0; i < values.size(); i++) {
						Object[] list = (Object[]) values.get(i);
						int column = 0;
						ActRequestListUser user = (ActRequestListUser) list[column++];
						CfgCodeDetails code = (CfgCodeDetails) list[column++];
						if (code.isSuccess()
								&& !ASCUtil
										.isNullOrEmpty(user.getAddressType())) {
							msg.append("\n\tuser: ");
							msg.append(user.getRequestUserId());
							if (ActRequestListUser.SENT_FROM_ADDRESS_TYPE
									.equalsIgnoreCase(user.getAddressType())) {
								sentFrom.add(user);
								msg.append(" sent From ");
							} else if (ActRequestListUser.REPLY_TO_ADDRESS_TYPE
									.equalsIgnoreCase(user.getAddressType())) {
								replyTo.add(user);
								msg.append(" reply To ");
							} else {
								msg.append(" not valid ");
							}
						}
					}
				} else {
					msg.append(" No values returned ");
				}
				if (!replyTo.isEmpty()) {
					recipients = replyTo;
					msg.append("\n\tReplay To Values Returned");
				} else if (!sentFrom.isEmpty()) {
					recipients = sentFrom;
					msg.append("\n\tSent From Values Returned");
				} else {
					msg.append("\n\tNo recipients found for request ");
				}
			} finally {
				ASCLogger.debug(this.getClass(), "getNotifyRecipients", msg
						.toString());
			}
		} else {

		}
		return recipients;
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#save(java.lang.Object)
	 */
	public void save(Object pojo) {
		super.save(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.IRequestTypeDAO#saveAlways(java.lang.Object)
	 */
	public void saveAlways(Object pojo) {
		super.save(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#deleteByID(java.lang.Object)
	 */
	public void deleteByID(Object pojo) {
		super.deleteByID(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#refresh(java.lang.Object)
	 */
	public void refresh(Object pojo) {
		super.refresh(pojo);
	}

}
